<?php
/**
 * 留言反馈
 * User: Administrator
 * Date: 2022/3/10
 * Time: 17:18
 */

namespace app\api\controller;
use app\common\controller\Api;
use think\Db;
use think\Request;

class Message extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    public function list()
    {
        if (!Request::instance()->isGet())
            $this->error('请求方式不正确');

        $page = input('post.page',1);
        $limit = input('post.limit',10);

        $list = Db::name('message')->field('id,name,content,reply_content')->paginate($limit,'',['page'=>$page]);
        if($list)
            $this->success('',$list);
        $this->error('暂无数据');
    }

    public function add()
    {
        if (!Request::instance()->isPost())
            $this->error('请求方式不正确');

        $params = input('post.');
        $params['createtime'] = time();
        $result = $this->validate($params,'Message');

        if (true !== $result) {
            // 验证失败 输出错误信息
            $this->error($result);
        }
        $res = Db::name('message')->insert($params);
        if($res)
            $this->success('留言成功');
        $this->error('留言失败');
    }
}